package com.xls.commonlibrary.http.exception;

import android.app.Activity;

import com.orhanobut.logger.Logger;

import java.io.IOException;

public class ThrowableUtil {
    private static volatile ThrowableUtil singleton = null;

    private ThrowableUtil() {
    }

    public static ThrowableUtil getInstance() {
        if (singleton == null) {
            synchronized (ThrowableUtil.class) {
                if (singleton == null) {
                    singleton = new ThrowableUtil();
                }
            }
        }
        return singleton;
    }

    public void handleThrowable(Throwable throwable) {

        Logger.e("=============handleThrowable===============");
        Logger.e(throwable.toString());

        if (throwable instanceof IOException) {
            //  处理IOException  http error
            // eg: 服务器不通  404 405啥的
            Logger.e("=============handleThrowable=======IOException========");

        } else if (throwable instanceof TokenInvalidException) {

            // todo 处理自定义错误：token失效问题
        } else if (throwable instanceof VersionInvalidException) {

            // todo 处理自定义错误：版本失效问题
        } else if (throwable instanceof DataNullException) {

            // todo 处理自定义错误：data is null 问题
        } else if (throwable instanceof DataFormatInvalidException) {

            Logger.e("=============handleThrowable=======DataFormatInvalidException========");

            // todo 处理自定义错误：data format is invalid 问题  eg:    data:"" is not {} or []
        }
        else if (throwable instanceof ClassCastException) {

            Logger.e("=============handleThrowable=======ClassCastException========");

            // todo 处理自定义错误：数据类型问题
        }


    }

    public void handleThrowable(Activity activity, Throwable throwable) {
        handleThrowable(throwable);

    }
}
